<?php

class Login extends MY_Controller{


	
	public function __construct()
	{
		parent::__construct();
		
		$this->load->model('account/account_m');
		
		$this->load->helper('account');
	
		
		$this->load->config('oauth2');
		
		
	}
	
	public function index()
	{
		$this->m_arrSub = array(
				'common/header'  =>  'index',
				'common/footer'  => 'index',
		);

		$this->getSub();
		
		$this->data['ajaxUrl'] = array(
				'login' => "index.php/account/login/tologin",
				'reg'   => "index.php/account/reg",
		);
		

		// 回调链接 ..
		$reUrl = $this->input->get('reUrl');
		
		$this->data['reUrl'] = $reUrl;

		
		$this->load->view('account/login',$this->data);
	}
	
	// 登录
	public function tologin()
	{
		$email = $this->input->post('email',true);
		
		$pwd   = $this->input->post('pwd',true);
		
		if (!filter_var($email,FILTER_VALIDATE_EMAIL)) 
		{
			
			$this->data['status'] = 0;
			$this->data['data']['msg'] = '邮箱不正确';
		}
		else
		{
			$arrUser = $this->account_m->getAccount(array('email' => $email,'pwd' => $pwd));
			
			
			if (empty($arrUser))
			{
				
			$this->data['status'] = 0;
			$this->data['data']['msg'] = '密码不正确';
			}
			else
			{
				
			$this->data['status'] = 0;
			$this->data['data']['msg'] = '登陆成功咯';
				
				// 更新平台数据表

		
				
				$this->setPlatform($arrUser['id']);
				
				
				$token    = createToken();
				$lastIP   = $this->input->server('REMOTE_ADDR'); // ip
				$lastTime = time();
				$toSet = array(
						'token'    => $token,
						'lastIP'   => $lastIP,
						'lastTime' => $lastTime,
						);
				
				
				$this->account_m->setAccount($arrUser['id'],$toSet);
				
				
				$arrUser['token']    = $token;
				$arrUser['lastIP']   = $lastIP;
				$arrUser['lastTime'] = $lastTime;
				
				$this->my_member->login($arrUser);
				
			}
		}
		
		echo json_encode($this->data);
	}
	
	// 注销
	public function logout()
	{
		$this->my_member->logout();
		
		$this->data['status'] = 1;
		
		echo json_encode($this->data);
	}
	
	
	
	
	
}
	